import pandas as pd
import util
import config
# import webapi

def preprocess_commit(data_out, path):
    util.base_preprocess(data_out, path, 'committer_login', 'commit')

def preprocess_issue(data_out, path):
    util.base_preprocess(data_out, path, 'user_login', 'issue')

def preprocess_PR(data_out, path):
    util.base_preprocess(data_out, path, 'PR_user_login', 'pullrequest')

def preprocess_issuecomment(data_out, path):
    util.base_preprocess(data_out, path, 'user_login', 'issuecomment')

def preprocess_commitcomment(data_out, path):
    util.base_preprocess(data_out, path, 'user_login', 'commitcomment')

# 请求数量大，会比较慢
# def preprocess_follow(data_out,base_url):
#     for k,v in data_out.items():
#         followers, following = webapi.get_follow_info(base_url.format(k))
#         v['followers'] = followers
#         v['following'] = following

def preprocess_contri(data_out):
    k_tuple = ('commit', 'issue', 'pullrequest', 'issuecomment', 'commitcomment', 'following', 'followers')
    temp = {}
    sum_all = 0
    for login,v in data_out.items():
        user_sum = 0
        for k in k_tuple:
            user_sum = user_sum + v[k]
        temp[login] = user_sum
        sum_all = sum_all + user_sum
    for login,v in data_out.items():
        v['contri'] = temp[login]/sum_all * 100


def main(config):
    for _ in config['repository_list']:
        data = {}
        preprocess_commit(data, config['data_path']['commit'].format(_.split('/')[-1])+'.csv')
        preprocess_issue(data, config['data_path']['issue'].format(_.split('/')[-1])+'.csv')
        preprocess_PR(data, config['data_path']['PR'].format(_.split('/')[-1])+'.csv')
        preprocess_issuecomment(data, config['data_path']['issuecomments'].format(_.split('/')[-1])+'.csv')
        preprocess_commitcomment(data, config['data_path']['commitcomments'].format(_.split('/')[-1])+'.csv')
        # preprocess_follow(data, config['base_url']+'/users/{}')
        preprocess_contri(data)
        data_df = util.hash_to_DataFrame(data)
        data_df.to_csv(config['data_path']['final'].format(_.split('/')[-1])+'.csv')
        # data_df.to_excel(config['data_path']['final'].format(_.split('/')[-1])+'.xlsx')


if __name__ == '__main__':
    _config = config.get_config()
    main(_config)